//
//  WBMainViewController.swift
//  SinaWeibo
//
//  Created by xu ge on 2017/5/24.
//  Copyright © 2017年 Gordon. All rights reserved.
//

import UIKit

class WBMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

    
        setupChildViewControllers()
        setComposeButton()
        
    }
    
    // 撰写按钮
    fileprivate lazy var composeButton:UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")

    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}


// MARK: -  设置界面
extension WBMainViewController {
    // 设置撰写按钮
    fileprivate func setComposeButton() {
        
        tabBar.addSubview(composeButton)
        // 计算Tabbar宽度
        let count = CGFloat(childViewControllers.count)
        let w = tabBar.bounds.width/count - 1

        //  82.8 = 414.0/5
        //  414x736     6/7Plus
        //  composeButton.frame = (165.6, 0.0, 82.8, 49.0)   减1后 (163.6, 0.0, 86.8, 49.0)
        //  tabBar.bounds = (0.0, 0.0, 414.0, 49.0)
        //  CGRect (x, y, width, height)
        //  x = dx      y = dy     width =  414.0 - dx*2    height = 49.0 - dy*2
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        print(composeButton.frame)

        
    }
   
    // 设置所有子控制器
    fileprivate func setupChildViewControllers() {
        
        let array = [
        ["clsName":"WBHomeViewController","title":"首页","imageName":"tabbar_home"],
        ["clsName":"WBMessageViewController","title":"消息","imageName":"tabbar_message_center"],
        ["clsName": "UIViewController"],
        ["clsName":"WBDiscoverViewController","title":"周边","imageName":"tabbar_discover"],
        ["clsName":"WBProfileViewController","title":"我","imageName":"tabbar_profile"],]
        
        //创建视图数组
        var arrayM = [UIViewController]()
        
        for dict in array {
            arrayM.append(controllerDict(dict: dict))
        }
        viewControllers = arrayM
    }
    
    //
    /// 用字典创建单个子控制器
    ///
    /// - Parameter dict: 信息字典【clsName,title,imageName】
    /// - Returns: 子控制器
    private func controllerDict(dict:[String:String]) -> UIViewController {
        
        // 获得字典内容
        guard let clsName = dict["clsName"],
            let title = dict["title"],
            let imageName = dict["imageName"],
            let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? WBBaseViewController.Type
        else {
            return UIViewController()
        }
        
        // 初始化视图控制器
        let vc = cls.init()
        // 设置标题和tabbar图标
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        // 设置Tabbar的标题字体颜色
        vc.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor:UIColor.orange], for: .highlighted)
        // 系统默认字体大小是12，修改字体大小，要设置Normal的大小
        vc.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.font:UIFont.systemFont(ofSize: 12)], for: UIControlState(rawValue: 0))
        // 给每个子控制器包装一个导航控制器
        let nav = WBNavigationController(rootViewController: vc)
        return nav
        
    }
}
